package com.hello.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hello.common.log.annotation.Log;
import com.hello.common.log.enums.BusinessType;
import com.hello.common.security.annotation.RequiresPermissions;
import com.hello.system.domain.RentalDeviceReviews;
import com.hello.system.service.IRentalDeviceReviewsService;
import com.hello.common.core.web.controller.BaseController;
import com.hello.common.core.web.domain.AjaxResult;
import com.hello.common.core.utils.poi.ExcelUtil;
import com.hello.common.core.web.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 设备评价Controller
 * 
 * @author hello
 * @date 2025-07-20
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/reviews")
public class RentalDeviceReviewsController extends BaseController
{
    private final IRentalDeviceReviewsService rentalDeviceReviewsService;

    /**
     * 查询设备评价列表
     */
    // @RequiresPermissions("system:reviews:list")
    @GetMapping("/list")
    public TableDataInfo list(RentalDeviceReviews rentalDeviceReviews)
    {
        log.info("======== 查询设备评价列表请求 ========");
        log.info("请求参数: {}", rentalDeviceReviews);
        
        startPage();
        List<RentalDeviceReviews> list = rentalDeviceReviewsService.selectRentalDeviceReviewsList(rentalDeviceReviews);
        TableDataInfo result = getDataTable(list);
        
        log.info("查询结果: 总记录数={}, 当前页数据量={}", result.getTotal(), list.size());
        log.info("======== 查询设备评价列表完成 ========");
        
        return result;
    }

    /**
     * 导出设备评价列表
     */
    // @RequiresPermissions("system:reviews:export")
    @Log(title = "设备评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RentalDeviceReviews rentalDeviceReviews)
    {
        log.info("======== 导出设备评价数据请求 ========");
        log.info("导出条件: {}", rentalDeviceReviews);
        
        List<RentalDeviceReviews> list = rentalDeviceReviewsService.selectRentalDeviceReviewsList(rentalDeviceReviews);
        log.info("导出数据量: {} 条记录", list.size());
        
        ExcelUtil<RentalDeviceReviews> util = new ExcelUtil<RentalDeviceReviews>(RentalDeviceReviews.class);
        util.exportExcel(response, list, "设备评价数据");
        
        log.info("======== 设备评价数据导出完成 ========");
    }

    /**
     * 获取设备评价详细信息
     */
    // @RequiresPermissions("system:reviews:query")
    @GetMapping(value = "/{reviewId}")
    public AjaxResult getInfo(@PathVariable("reviewId") Long reviewId)
    {
        log.info("======== 获取设备评价详情请求 ========");
        log.info("评价ID: {}", reviewId);
        
        RentalDeviceReviews review = rentalDeviceReviewsService.selectRentalDeviceReviewsByReviewId(reviewId);
        log.info("查询结果: {}", review != null ? "找到评价信息" : "未找到对应评价");
        
        log.info("======== 获取设备评价详情完成 ========");
        return success(review);
    }

    /**
     * 新增设备评价
     */
    // @RequiresPermissions("system:reviews:add")
    @Log(title = "设备评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RentalDeviceReviews rentalDeviceReviews)
    {
        log.info("======== 新增设备评价请求 ========");
        log.info("新增数据: {}", rentalDeviceReviews);
        
        int result = rentalDeviceReviewsService.insertRentalDeviceReviews(rentalDeviceReviews);
        log.info("新增结果: {}", result > 0 ? "成功" : "失败");
        
        log.info("======== 新增设备评价完成 ========");
        return toAjax(result);
    }

    /**
     * 修改设备评价
     */
    // @RequiresPermissions("system:reviews:edit")
    @Log(title = "设备评价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RentalDeviceReviews rentalDeviceReviews)
    {
        log.info("======== 修改设备评价请求 ========");
        log.info("修改数据: {}", rentalDeviceReviews);
        
        int result = rentalDeviceReviewsService.updateRentalDeviceReviews(rentalDeviceReviews);
        log.info("修改结果: {}", result > 0 ? "成功" : "失败");
        
        log.info("======== 修改设备评价完成 ========");
        return toAjax(result);
    }

    /**
     * 删除设备评价
     */
    // @RequiresPermissions("system:reviews:remove")
    @Log(title = "设备评价", businessType = BusinessType.DELETE)
	@DeleteMapping("/{reviewIds}")
    public AjaxResult remove(@PathVariable Long[] reviewIds)
    {
        log.info("======== 删除设备评价请求 ========");
        log.info("删除评价ID列表: {}", java.util.Arrays.toString(reviewIds));
        log.info("删除数量: {} 个评价记录", reviewIds.length);
        
        int result = rentalDeviceReviewsService.deleteRentalDeviceReviewsByReviewIds(reviewIds);
        log.info("删除结果: {}", result > 0 ? "成功" : "失败");
        
        log.info("======== 删除设备评价完成 ========");
        return toAjax(result);
    }
}
